(function() {
    document.body.ondrop = function(event) {
        event.preventDefault();
        event.stopPropagation();
    };
    var diyData = {};
    var options = {};

    function diyPhone(temp, data, opts) {
        diyData = temp;
        this.init(data, opts)
    }
		
    diyPhone.prototype = {
        init: function(_0xf314x8, opts) {
            new Vue({
                el: '#app',
                data: {
                    diyData: _0xf314x8,
                    selectedIndex: -1,
                    curItem: {},
                    opts: opts
                },
                methods: {
					//添加组件
                    onAddItem: function(temp) {
                        var _0xf314x8 = $.extend(true, {}, diyData[temp]);
                        this.diyData.items.push(_0xf314x8);
                        this.onEditer(this.diyData.items.length - 1)
                    },
                    onDragItemEnd: function(_0xf314xa) {
                        this.onEditer(_0xf314xa.newIndex)
                    },
                    onEditer: function(_0xf314xb) {
                        this.selectedIndex = _0xf314xb;
                        this.curItem = this.selectedIndex === 'page' ? this.diyData.page : this.diyData.items[this.selectedIndex];
                        this.initEditor()
                    },
                    onDeleleItem: function(_0xf314xb) {
                        var _0xf314xc = this;
                        layer.confirm('确定要删除吗？',
                            function(_0xf314xd) {
                                _0xf314xc.diyData.items.splice(_0xf314xb, 1);
                                _0xf314xc.selectedIndex = -1;
                                layer.close(_0xf314xd)
                            })
                    },
                    onEditorSelectImage: function(banner, imgUrl) {
                        $.fileLibrary({
                            type: 'image',
                            done: function(_0xf314xf) {
                                banner[imgUrl] = _0xf314xf[0].file_path
                            }
                        })
                    },
                    onEditorResetColor: function(_0xf314x10, _0xf314x11, _0xf314x12) {
                        _0xf314x10[_0xf314x11] = _0xf314x12
                    },
                    onEditorDeleleData: function(_0xf314xb, _0xf314x13) {
                        if (this.diyData.items[_0xf314x13].data.length <= 1) {
                            layer.msg('至少保留一个', {
                                anim: 6
                            });
                            return false
                        };
                        this.diyData.items[_0xf314x13].data.splice(_0xf314xb, 1)
                    },
                    onEditorAddData: function() {
                        var _0xf314x14 = $.extend(true, {}, diyData[this.curItem.type].data[0]);
                        this.curItem.data.push(_0xf314x14)
                    },
                    initEditor: function() {
                        this.$nextTick(function() {
                            if (options.hasOwnProperty('key')) {
                                options.destroy()
                            };
                            this.editorHtmlComponent();
                            if (this.curItem.type === 'richText') {
                                this.onRichText(this.curItem)
                            };
                            if (this.curItem.type === 'goods' || this.curItem.type === 'sharingGoods') {
                                this.editorSelectGoods(this.curItem)
                            }
                        })
                    },
                    editorHtmlComponent: function() {
                        var diyeditor = $(this.$refs['diy-editor']);
                        diyeditor.find('input[type=checkbox], input[type=radio]').uCheck();
                    },
                    editorSelectGoods: function(_0xf314x16) {
                        var _0xf314x17 = $(this.$refs.jSelectGoods);
                        var _0xf314x18 = {
                            goods: 'goods/lists&status=10',
                            sharingGoods: 'sharing.goods/lists&status=10'
                        };
                        _0xf314x17.selectData({
                            title: '选择商品',
                            uri: _0xf314x18[_0xf314x16.type],
                            duplicate: false,
                            dataIndex: goods_id,
                            done: function(_0xf314x8) {
                                _0xf314x8.forEach(function(_0xf314x19) {
                                    _0xf314x16.data.push(_0xf314x19)
                                })
                            },
                            getExistData: function() {
                                var _0xf314x1a = [];
                                _0xf314x16.data.forEach(function(_0xf314x1b) {
                                    if (_0xf314x1b.hasOwnProperty(goods_id)) {
                                        _0xf314x1a.push(_0xf314x1b.goods_id)
                                    }
                                });
                                return _0xf314x1a
                            }
                        })
                    },
                    onRichText: function(_0xf314x16) {
                        options = UM.getEditor('ume-editor', {
                            initialFrameWidth: 375,
                            initialFrameHeight: 400
                        });
                        options.ready(function() {
                            options.setContent(_0xf314x16.params.content);
                            options.addListener('contentChange', function() {
                                _0xf314x16.params.content = options.getContent()
                            })
                        })
                    },
                    onSubmit: function() {
                        if (this.diyData.items.length <= 0) {
                            layer.msg('至少存在一个组件', {
                                anim: 6
                            });
                            return false
                        };
                        $.post('', {
                            data: this.diyData
                        }, function(_0xf314x1c) {
                            _0xf314x1c.code === 1 ? $.show_success(_0xf314x1c.msg, _0xf314x1c.url) : $.show_error(_0xf314x1c.msg)
                        })
                    }
                }
            })
        }
    };
    window.diyPhone = diyPhone
})(window);